package cn.zw.msmp.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.zw.msmp.common.util.CommonUtils;
import cn.zw.msmp.controller.vo.BaseVO;
import cn.zw.msmp.controller.vo.CustomerVO;
import cn.zw.msmp.pojo.CustomerInfo;
import cn.zw.msmp.service.ICustomerService;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/customer/")
public class CustomerController {
	
	private int page = 1;
	private int rows = 10;
	
	@Autowired
	@Qualifier("customerService")
	private ICustomerService service;
	
	@RequestMapping("/add")
	public String add(@RequestBody String content) {
		Map<String, String> map = CommonUtils.convertReqParams(content);
		CustomerInfo info = new CustomerInfo();
		info.setCustomerName(map.get("customerName"));
		info.setMobile(map.get("mobile"));
		info.setRemark(map.get("remark"));
		info.setAddress(map.get("address"));
		service.insert(info);
		return "0";
	}
	
	@RequestMapping("/list")
	public BaseVO<List<CustomerVO>> query(@RequestBody String condition) {
		CustomerInfo queryInfo = new CustomerInfo();
		if(StringUtils.isNotEmpty(condition)){
			Map<String, String> map = CommonUtils.convertReqParams(condition);
			queryInfo.setCustomerName(map.get("customerName"));
			queryInfo.setMobile(map.get("mobile"));
			page = StringUtils.isNotEmpty(map.get("page")) ? Integer.parseInt(map.get("page")) : page;
			rows = StringUtils.isNotEmpty(map.get("rows")) ? Integer.parseInt(map.get("rows")) : rows; 
		}
		PageInfo<CustomerInfo> pageInfos = service.queryCustomers(queryInfo, page, rows);
		List<CustomerInfo> infos = pageInfos.getList();
		rows = (int) (null != pageInfos ? pageInfos.getTotal() : rows);
		List<CustomerVO> vos = new ArrayList<>();
		if(null != infos && infos.size() > 0) {
			for(CustomerInfo info : infos) {
				vos.add(new CustomerVO(info));
			}
		}
		BaseVO<List<CustomerVO>> result = new BaseVO<List<CustomerVO>>(page, rows, pageInfos.getTotal(), vos);
		return result;
	}
	
	@RequestMapping("/delete")
	public String delete(@RequestBody String ids) {
		try {
			ids = URLDecoder.decode(ids.replaceAll("=", ""), "UTF-8");
			List<Integer> list = new ArrayList<>();
			String[] tmp = ids.split(",");
			for(String id : tmp) {
				list.add(Integer.valueOf(id));
			}
			service.delete(list);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "0";
	}
	
	@RequestMapping("/update")
	public String update(@RequestBody String content) {
		Map<String, String> map = CommonUtils.convertReqParams(content);
		CustomerInfo info = new CustomerInfo();
		info.setId(Integer.valueOf(map.get("id")));
		info.setCustomerName(map.get("customerName"));
		info.setMobile(map.get("mobile"));
		info.setRemark(map.get("remark"));
		info.setAddress(map.get("address"));
		service.update(info);
		return "0";
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	
}
